#!/bin/bash

##*************
## desc:增加容错性
## auther:hongxing.fan
## date:2015年06月16日 星期二
##*************

cur_date=$(date +"%Y%m%d" -d "-1 day")
while getopts 'd:' opt; do
	case $opt in
		d)
			cur_date=$OPTARG
			;;
		*)
			echo "usage:sh create_ssn_partition.sh -d 20150503"
			exit 1
			;;
	esac
done

ssn_log=/user/hive/warehouse/pass_data.db/ssn_log
source /home/hadoop/.bashrc
file=$(hdfs dfs -ls /user/hive/warehouse/pass_data.db/ssn_log/*/dt=$cur_date)
stat=$?
tryTime=24
while [[ $stat -ne 0 && $tryTime -gt 0 ]]
do
	tryTime=$(expr $tryTime - 1)
	sleep $(expr 1 \* 60 \* 60)
	file=$(hdfs dfs -ls /user/hive/warehouse/pass_data.db/ssn_log/*/dt=$cur_date)
	stat=$?
done
partitions=$(hdfs dfs -ls /user/hive/warehouse/pass_data.db/ssn_log/tpl=*/dt=$cur_date | awk '{print $8}' | awk -F"tpl=|/dt=" '{print $2}' | sort | uniq)
for tpl in ${partitions[*]}
do
	$(hive -e "use pass_data;alter table ssn_log add partition(tpl='$tpl',dt=$cur_date) location 'hdfs:///user/hive/warehouse/pass_data.db/ssn_log/tpl=$tpl/dt=$cur_date';")
done
